package com.ruoyi.web.controller.fireman;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.fireman.domain.TMenu;
import com.ruoyi.fireman.service.ITMenuService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 首页菜单Controller
 * 
 * @author ruoyi
 * @date 2021-08-24
 */
@Controller
@RequestMapping("/fireman/fireman")
public class TMenuController extends BaseController
{
    private String prefix = "fireman/fireman";

    @Autowired
    private ITMenuService tMenuService;

    @RequiresPermissions("fireman:fireman:view")
    @GetMapping()
    public String fireman()
    {
        return prefix + "/fireman";
    }

    /**
     * 查询首页菜单列表
     */
    @RequiresPermissions("fireman:fireman:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TMenu tMenu)
    {
        startPage();
        List<TMenu> list = tMenuService.selectTMenuList(tMenu);
        return getDataTable(list);
    }

    /**
     * 导出首页菜单列表
     */
    @RequiresPermissions("fireman:fireman:export")
    @Log(title = "首页菜单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TMenu tMenu)
    {
        List<TMenu> list = tMenuService.selectTMenuList(tMenu);
        ExcelUtil<TMenu> util = new ExcelUtil<TMenu>(TMenu.class);
        return util.exportExcel(list, "首页菜单数据");
    }

    /**
     * 新增首页菜单
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        QueryWrapper<TMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().isNull(TMenu::getParentId);
        mmap.put("parents", tMenuService.list(queryWrapper));
        return prefix + "/add";
    }

    /**
     * 新增保存首页菜单
     */
    @RequiresPermissions("fireman:fireman:add")
    @Log(title = "首页菜单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TMenu tMenu)
    {
        return toAjax(tMenuService.insertTMenu(tMenu));
    }

    /**
     * 修改首页菜单
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TMenu tMenu = tMenuService.getById(id);
        mmap.put("tMenu", tMenu);
        QueryWrapper<TMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().isNull(TMenu::getParentId);
        mmap.put("parents", tMenuService.list(queryWrapper));
        return prefix + "/edit";
    }

    /**
     * 修改保存首页菜单
     */
    @RequiresPermissions("fireman:fireman:edit")
    @Log(title = "首页菜单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TMenu tMenu)
    {
        return toAjax(tMenuService.updateTMenu(tMenu));
    }

    /**
     * 删除首页菜单
     */
    @RequiresPermissions("fireman:fireman:remove")
    @Log(title = "首页菜单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tMenuService.deleteTMenuByIds(ids));
    }

}
